<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\CustHub\Mapper\ChRoomInviteActiveRoomMapper;
use Mine\Abstracts\AbstractService;
use Mine\Annotation\Transaction;

class ChRoomInviteActiveRoomService extends AbstractService
{
    public $mapper;

    public function __construct(ChRoomInviteActiveRoomMapper $mapper)
    {
        $this->mapper = $mapper;
    }

    public function getActiveRoomPageList(array $params)
    {
        $data = $this->mapper->getActiveRoomPageList($params);
        // 通过分页后的数据来进行按 robot_id 分组
        $roomsData = $data->getCollection()->groupBy('robot_id');

        $result = [];
        $group_user_count  = $params['group_user_count'];


        // 遍历每个 robot_id 的分组数据
        foreach ($roomsData as $robot_id => $roomGroup) {
            $robot = $roomGroup->first()->robot;

            $roomGroup = $roomGroup->sortBy('sort');
            $exists = $roomGroup->contains(function ($item) {
                return $item->status === 1;
            });
            if(!$exists){
                $roomGroup->first(function ($item) {
                    $item->status = 1;  // 修改第一个元素的 status
                    return $item;
                });
            }

            $robotData = [
                'robot_id' => $robot_id,
                'robot_nickname' => $robot['nickname'] ?? '',
                'robot_avatar' => $robot['avatar'] ?? '',
                'robot_user_id' => $robot['user_id'] ?? '',
                'room_list' => $roomGroup->map(function ($item) use ($group_user_count){
                    $roomMembersCount = $item->room->room_member_count ?? '';

                    if($roomMembersCount >= $group_user_count){
                        $roomStatus = 1;
                    }else{
                        $roomStatus = 0;
                    }

                    return [
                        'id' => $item->id,
                        'room_name' => $item->room->room_name ?? '',
                        'room_wxid' => $item->room_wxid, // 群的微信ID
                        'room_member_count' => $item->room->room_member_count ?? '',
                        'genders' => $item->genders, // 性别信息
                        'sort' => $item->sort, // 排序字段
                        'room_status' => $roomStatus,//群状态
                        'status' => $item->status, // 状态
                    ];
                })->toArray(),
            ];

            $result[] = $robotData;
        }


        return [
            'items' => $result,
            'pageInfo' => [
                'total' => $data->total(),
                'currentPage' => $data->currentPage(),
                'totalPage' => $data->lastPage(),
            ]
        ];
    }

    /**
     * 设置进粉状态
     * @param array $params
     * @param int $type
     * @return bool
     */
    #[Transaction]
    public function setActiveRoomStatus(array $params,int $type): bool
    {
        $rst = $this->mapper->model::query()
            ->where('scheme_id',$params['scheme_id'])
            ->where('scheme_type',$type)
            ->update([
                'status' => 0
            ]);

        if(!$rst){
            return false;
        }

        $rst = $this->mapper->model::query()
            ->where('scheme_id',$params['scheme_id'])
            ->where('robot_id',$params['robot_id'])
            ->where('room_wxid',$params['room_wxid'])
            ->update([
                'status' => 1
            ]);
        if(!$rst){
            return false;
        }
        return true;
    }
}